//
//  WWNetworkTools.m
 
//
//  Created by 张湾湾 on 16/7/24.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWNetworkTools.h"
typedef NS_ENUM(NSUInteger, NetworkToolsType) {
    NetworkToolsTypeGet,
    NetworkToolsTypePost,
   
};
@implementation WWNetworkTools

//全局变量
    static WWNetworkTools * instance;
//创建单例模型
+ (instancetype)sharedTools{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        //1.实例化
        instance = [[WWNetworkTools alloc] initWithSessionConfiguration:configuration];
        
        //2.可接受格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        
        //3.请求格式（默认：二进制）
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //4.超时
        instance.requestSerializer.timeoutInterval = 10.0;

        
    });
    
    return instance;
    
 }
#pragma mark - 封装调用
- (void)loadData:(NetworkToolsType)type parmeters:(id)parmeters urlString:(NSString*)urlString success:(void(^)(id response))success failure:(void(^)(NSError*error))failure{
    if (type == NetworkToolsTypeGet) {
        [[WWNetworkTools sharedTools]GET:urlString parameters:parmeters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%@",responseObject);
//            success(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"%@",error);
        }];
    }else if(type == NetworkToolsTypePost){
        [[WWNetworkTools sharedTools]POST:urlString parameters:parmeters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"post %@",responseObject);
//            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"post%@",error);
        }];
        
    }
}


#pragma mark - 通过参数调用
 
@end
